#include "trayToolKit.h"

TrayToolKit::TrayToolKit(int newType, double newB, double newH, double newM, double newI, double newF, double newP) {

  setType(newType);
  setVar(newB, VAR_B);
  setVar(newH, VAR_H);
  setVar(newH, VAR_HR);
  setVar(newM, VAR_M);
  setVar(newF, VAR_F);
  setVar(newI, VAR_I);
  setVar(newI, VAR_IR);
  setVar(newP, VAR_P);
  
  insG     = DEFG;
  insA     = DEFA;
  insQ20   = DEFQ20;

  setEnvironment(DEFCONCRETE,	VAR_CONCRETE);
  setEnvironment(DEFREGION, 	VAR_REGION);
  setEnvironment(DEFTYPEUPSIDE, VAR_TYPEUPSIDE);
  setEnvironment(DEFTIMERAIN, 	VAR_TIMERAIN);
  setEnvironment(DEFCLCONCRETE, VAR_CLCONCRETE);

  updateN();
  updateCnMrCy();
  updateZmid();
  updateKpf();
  updateKpt();
  updateVd();
  updateVs();
  updateVr();
  
} //TrayToolKit::TrayToolKit(int newType, double newB, double newH, double newM, double newI, double newF, double newP)

int TrayToolKit::type() {

  return insType;

} //int TrayToolKit::type()

void TrayToolKit::setType(int newType) {

  insType = newType;

} //void TrayToolKit::setType(int newType)

double TrayToolKit::getVar(int nameVar) {

  switch (nameVar) {

     case VAR_B:	     return insB;
     case VAR_H: 	     return insH;
     case VAR_HR: 	   return insHr;
     case VAR_M: 	     return insM;
     case VAR_F: 	     return insF;
     case VAR_I: 	     return insI;
     case VAR_IR: 	   return insIr;
     case VAR_P: 	     return insP;
     case VAR_G: 	     return insG;
     case VAR_A: 	     return insA;
     case VAR_Q20: 	   return insQ20;
     case VAR_N:   	   return insN;
     case VAR_CN:      return insCn;
     case VAR_MR:  	   return insMr;
     case VAR_CY:  	   return insCy;
     case VAR_ZMID:	   return insZmid;
     case VAR_KPF:     return insKpf;
     case VAR_KPT:     return insKpt;
     case VAR_HRAIN:   return insHrain;
     case VAR_VD:      return insVd;
     case VAR_VS:      return insVs;
     case VAR_VR:      return insVr;

  } //switch (nameVar)

  return 0;

} //double TrayToolKit::getVar(int nameVar)

void TrayToolKit::setVar(double newVal, int nameVar) {

  switch (nameVar) {

     case VAR_B:	    insB		  = newVal; updateHrain(); /*updateVs(); updateVr();*/	break;
     case VAR_H:   	  insH    	= newVal; updateHrain(); 				                  break;
     case VAR_HR:   	insHr   	= newVal; updateVs();    updateVr();	            break;
     case VAR_M:   	  insM    	= newVal; updateHrain(); /*updateVr();*/          break;
     case VAR_I:   	  insI    	= newVal; updateHrain(); /*updateVs(); updateVr();*/	break;
     case VAR_IR:   	insIr   	= newVal; updateVr();	   updateVs(); updateVr();  break;
     case VAR_P:   	  insP    	= newVal; updateHrain(); 	     			              break;
     case VAR_F:   	  insF    	= newVal; updateKpt();   /*updateHrain();*/	      break;
     case VAR_G:   	  insG    	= newVal; updateHrain();				                  break;
     case VAR_Q20: 	  insQ20  	= newVal; updateHrain();				                  break;
     case VAR_N:   	  insN    	= newVal; updateHrain();				                  break;
     case VAR_ZMID:	  insZmid 	= newVal; updateHrain();				                  break;
     case VAR_CN:   	insCn   	= newVal; updateHrain();				                  break;
     case VAR_MR:  	  insMr   	= newVal; updateHrain();				                  break;
     case VAR_CY:  	  insCy   	= newVal; updateHrain();				                  break;
     case VAR_KPF:   	insKpf 		= newVal; updateHrain();				                  break;
     case VAR_KPT:   	insKpt 		= newVal; updateHrain();				                  break;
     case VAR_HRAIN:  insHrain 	= newVal; updateVd();	   updateVs(); updateVr();  break;
     case VAR_VD:   	insVd 		= newVal; 						                            break;
     case VAR_VS:   	insVs 		= newVal; 						                            break;
     case VAR_VR:   	insVr 		= newVal; 						                            break;
     case VAR_A:   	  insA    	= newVal; updateHrain();				                  break;

  } //switch (nameVar)

} //void TrayToolKit::setVar(double newVal, int nameVar)

QString TrayToolKit::getDimVarHtml(int nameVar) {

  switch (nameVar) {

     case VAR_B:		return B_DIM_HTML; break;
     case VAR_H:   		return H_DIM_HTML; break;
     case VAR_M:   		return EMPTY_DIM; break;
     case VAR_I:   		return EMPTY_DIM; break;
     case VAR_P:   		return P_DIM_HTML; break;
     case VAR_F:   		return F_DIM_HTML; break;

     case VAR_G:   		return G_DIM_HTML; break;
     case VAR_A:   		return EMPTY_DIM; break;
     case VAR_Q20: 		return Q20_DIM_HTML; break;

     case VAR_N:   		return EMPTY_DIM; break;
     case VAR_CN:   		return EMPTY_DIM; break;
     case VAR_MR:  		return EMPTY_DIM; break;
     case VAR_CY:  		return EMPTY_DIM; break;
     case VAR_ZMID:		return EMPTY_DIM; break;
     case VAR_KPF:   		return EMPTY_DIM; break;
     case VAR_KPT:   		return EMPTY_DIM; break;
     case VAR_V:   		return V_DIM_HTML; break;

     case VAR_AREA:		return S_DIM_HTML; break;
     case VAR_PERIMETER:	return X_DIM_HTML; break;
     case VAR_RADIUS:		return R_DIM_HTML; break;
     case VAR_SHEZY:		return C_DIM_HTML; break;
     case VAR_QCON:		return Q_DIM_HTML; break;

     case VAR_L:   		return L_DIM_HTML; break;
     case VAR_ANGLE:   		return EMPTY_DIM; break;
     case VAR_TIMERAIN:		return T_DIM_HTML; break;

     case VAR_EMPTY:   		return EMPTY_DIM; break;
   
  } //switch (nameVar)

  return "";

} //QString TrayToolKit::getDimVarHtml(int nameVar)

QString TrayToolKit::getDimVarText(int nameVar) {

  switch (nameVar) {

     case VAR_B:		return B_DIM_TEXT; break;
     case VAR_H:   		return H_DIM_TEXT; break;
     case VAR_M:   		return EMPTY_DIM; break;
     case VAR_I:   		return EMPTY_DIM; break;
     case VAR_P:   		return P_DIM_TEXT; break;
     case VAR_F:   		return F_DIM_TEXT; break;

     case VAR_G:   		return G_DIM_TEXT; break;
     case VAR_A:   		return EMPTY_DIM; break;
     case VAR_Q20: 		return Q20_DIM_TEXT; break;

     case VAR_N:   		return EMPTY_DIM; break;
     case VAR_CN:   		return EMPTY_DIM; break;
     case VAR_MR:  		return EMPTY_DIM; break;
     case VAR_CY:  		return EMPTY_DIM; break;
     case VAR_ZMID:		return EMPTY_DIM; break;
     case VAR_KPF:   		return EMPTY_DIM; break;
     case VAR_KPT:   		return EMPTY_DIM; break;
     case VAR_V:   		return V_DIM_TEXT; break;

     case VAR_AREA:		return S_DIM_TEXT; break;
     case VAR_PERIMETER:	return X_DIM_TEXT; break;
     case VAR_RADIUS:		return R_DIM_TEXT; break;
     case VAR_SHEZY:		return C_DIM_TEXT; break;
     case VAR_QCON:		return Q_DIM_TEXT; break;

     case VAR_L:   		return L_DIM_TEXT; break;
     case VAR_ANGLE:   		return EMPTY_DIM; break;
     case VAR_TIMERAIN:		return T_DIM_TEXT; break;
     case VAR_EMPTY:   		return EMPTY_DIM; break;
   
  } //switch (nameVar)

  return "";

} //QString TrayToolKit::getDimVarText(int nameVar)

int TrayToolKit::environment(int nameVar) {

  switch (nameVar) {

     case VAR_CONCRETE:		return insTypeConcrete;		break;
     case VAR_REGION:		return insRegion;		break;
     case VAR_TYPEUPSIDE:	return insTypeUpside;		break;
     case VAR_TIMERAIN:		return insTimeRain;		break;
     case VAR_CLCONCRETE:	return insClassConcrete;	break;
  
  } //switch (nameVar)

  return 0;

} //int TrayToolKit::environment(int nameVar)

void TrayToolKit::setEnvironment(int newVal, int nameVar) {

  switch (nameVar) {

     case VAR_CONCRETE:		insTypeConcrete = newVal;  updateN();	   break;
     case VAR_REGION:		insRegion = newVal;	   updateCnMrCy(); break;
     case VAR_TYPEUPSIDE:	insTypeUpside = newVal;	   updateZmid();   break;
     case VAR_TIMERAIN:		insTimeRain = newVal;	   updateKpt();	   break;
     case VAR_CLCONCRETE:	insClassConcrete = newVal; updateVd();	   break;
  
  } //switch (nameVar)

} //void TrayToolKit::setEnvironment(int newVal, int nameVar)

double TrayToolKit::areaLiveProfile(double H, double B, double M) {

  if (H == 0)
    H = getVar(VAR_H);
  
  if (B == 0)
    B = getVar(VAR_B);
  
  if (M == 0)
    M = getVar(VAR_M);

  int Type = type();

  double fAreaLiveProfile = 0;

  if      (Type == SYMTTRAPEZE)
    fAreaLiveProfile  =       M * pow(H, 2) + H * B;

  else if (Type == ASYMTTRAPEZE)
    fAreaLiveProfile  = 0.5 * M * pow(H, 2) + H * B;

  else if (Type == RECTANGLE)
    fAreaLiveProfile  =			      H * B;

  return fAreaLiveProfile; 

} //double TrayToolKit::areaLiveProfile(double H, double B, double M)

double TrayToolKit::areaLiveProfile_cr() {

  return areaLiveProfile(crHeightTrapTray());

} //double TrayToolKit::areaLiveProfile_cr()

double TrayToolKit::areaLiveProfile_r() {

  return areaLiveProfile(getVar(VAR_HR));

} //double TrayToolKit::areaLiveProfile_r()

double TrayToolKit::moistenedPerimeter(double H, double B, double M) {

  double fMoistenedPerimeter = 0;

  if (H == 0)
    H = getVar(VAR_H);
  
  if (B == 0)
    B = getVar(VAR_B);
  
  if (M == 0)
    M = getVar(VAR_M);

  int Type = type();

  if      (Type == SYMTTRAPEZE)
    fMoistenedPerimeter  = 2 * sqrt( M * pow(H, 2) + pow(H, 2) ) + B;

  else if (Type == ASYMTTRAPEZE)
    fMoistenedPerimeter  =     sqrt( M * pow(H, 2) + pow(H, 2) ) + B +     H;

  else if (Type == RECTANGLE)
    fMoistenedPerimeter  =     	   	     		       	   B + 2 * H;

  return fMoistenedPerimeter;

} //double TrayToolKit::moistenedPerimeter(double H, double B, double M)

double TrayToolKit::moistenedPerimeter_cr() {

  return moistenedPerimeter(crHeightTrapTray());

} //double TrayToolKit::moistenedPerimeter_cr()

double TrayToolKit::moistenedPerimeter_r() {

  return moistenedPerimeter(getVar(VAR_HR));

} //double TrayToolKit::moistenedPerimeter_r()

double TrayToolKit::hydraulicRadius(double H, double S, double X) {

  double fHydraulicRadius = 0;

  if (H == 0)
    H = getVar(VAR_H);

  if (S == 0)
    S = areaLiveProfile(H);

  if (X == 0)
    X = moistenedPerimeter(H);

  fHydraulicRadius = S / X;

  return fHydraulicRadius;

} //double TrayToolKit::hydraulicRadius(double H, double S, double X)

double TrayToolKit::hydraulicRadius_cr() {

  return hydraulicRadius(crHeightTrapTray());

} //double TrayToolKit::hydraulicRadius_cr()

double TrayToolKit::hydraulicRadius_r() {

  return hydraulicRadius(getVar(VAR_HR));

} //double TrayToolKit::hydraulicRadius_r()

double TrayToolKit::shezy(double H, double R, double N) {

  double fShezy = 0;

  if (H == 0)
    H = getVar(VAR_H);

  if (R == 0)
    R = hydraulicRadius(H);

  if (N == 0)
    N = getVar(VAR_N);

  //Коэфициент Шези
  fShezy = (1 / N)  * pow(  R, ( 2.5 * sqrt(N) - 0.13 - 0.75 * sqrt(R) * (sqrt(N) - 0.1) )  );

  return fShezy;

} //double TrayToolKit::shezy(double H, double R, double N)

double TrayToolKit::shezy_cr() {
  
  return shezy(crHeightTrapTray());

} //double TrayToolKit::shezy_cr()

double TrayToolKit::shezy_r() {

  return shezy(getVar(VAR_HR));

} //double TrayToolKit::shezy_r()

double TrayToolKit::waterConsumption(double S, double R, double C, double I) {

  double fWaterConsumption = 0;

  if (S == 0)
    S = areaLiveProfile();

  if (R == 0)
    R = hydraulicRadius();

  if (C == 0)
    C = shezy();

  if (I == 0)
    I = getVar(VAR_I);
  
  fWaterConsumption = S * C * sqrt(R * I);

  return fWaterConsumption;

} //double TrayToolKit::waterConsumption(double S, double R, double C, double I)

double TrayToolKit::calcWaterConsumption(double S, double R, double C, double Ir) {

  double fcalcWaterConsumption = 0;

  if (S == 0)
    S = areaLiveProfile(getVar(VAR_HR));

  if (R == 0)
    R = hydraulicRadius(getVar(VAR_HR));

  if (C == 0)
    C = shezy(getVar(VAR_HR));

  if (Ir == 0)
    Ir = getVar(VAR_IR);
  
  fcalcWaterConsumption = S * C * sqrt(R * Ir);

  return fcalcWaterConsumption;

} //double TrayToolKit::calcWaterConsumption(double S, double R, double C, double Ir)

double TrayToolKit::crWaterConsumption(double S, double R, double C, double I) {

  double fcrWaterConsumption = 0;

  if (S == 0)
    S = areaLiveProfile(crHeightTrapTray());

  if (R == 0)
    R = hydraulicRadius(crHeightTrapTray());

  if (C == 0)
    C = shezy(crHeightTrapTray());

  if (I == 0)
    I = getVar(VAR_I);
  
  fcrWaterConsumption = S * C * sqrt(R * I);

  return fcrWaterConsumption;

} //double TrayToolKit::crWaterConsumption(double S, double R, double C, double I)

double TrayToolKit::recWaterConsumption() {

  if (type() == RECTANGLE) return waterConsumption()*0.75;
  else 	     		   return waterConsumption()*0.70;

} //double TrayToolKit::recWaterConsumption()

double TrayToolKit::recHeightTrapTray() {

  if (type() == RECTANGLE) return getVar(VAR_H)*0.75;
  else 	     		   return getVar(VAR_H)*0.70;

} //double TrayToolKit::recHeightTrapTray()

double TrayToolKit::crHeightRectTray(double Q, double B, double A, double G) {
  
  double fCrHeightRectTray = 0;

  if (Q == 0)
    Q = waterConsumption();

  if (B == 0)
    B = getVar(VAR_B);

  if (A == 0)
    A = getVar(VAR_A);
  
  if (G == 0)
    G = getVar(VAR_G);

  //Критической высоты для прямоугольного лотка
  fCrHeightRectTray = cbrt( (A * pow(Q,2)) / (G * B) );
  
  return fCrHeightRectTray;

} //double TrayToolKit::crHeightRectTray(double Q, double B, double A, double G)

double TrayToolKit::crHeightTrapTray(double Hcrp, double B, double M) {
  
  double fCrHeightTrapTray = 0;

  if (Hcrp == 0)
    Hcrp = crHeightRectTray();

  if (B == 0)
    B = getVar(VAR_B);

  if (M == 0)
    M = getVar(VAR_M);

  int Type = type();

  if      (Type == SYMTTRAPEZE)
    fCrHeightTrapTray  =                   (  1 - (M * Hcrp) / (3 * B) + 0.105 * (M * Hcrp) / B  ) * Hcrp;

  else if (Type == ASYMTTRAPEZE)
    fCrHeightTrapTray  = Hcrp + ( Hcrp  -  (  1 - (M * Hcrp) / (3 * B) + 0.105 * (M * Hcrp) / B  ) * Hcrp  ) / 2;

  else if (Type == RECTANGLE)
    fCrHeightTrapTray  = Hcrp;

  return fCrHeightTrapTray;

} //double TrayToolKit::crHeightTrapTray(double Hcrp, double B, double M)

double TrayToolKit::widthForCrHeight(double Hcr, double B, double M) {

  double fWidthForCrHeight = 0;

  if (Hcr == 0)
    Hcr = crHeightTrapTray();

  if (B == 0)
    B = getVar(VAR_B);

  if (M == 0)
    M = getVar(VAR_M);

  int Type = type();

  if      (Type == SYMTTRAPEZE)
    fWidthForCrHeight  = 2 * M * Hcr + B;

  else if (Type == ASYMTTRAPEZE)
    fWidthForCrHeight  =     M * Hcr + B;

  else if (Type == RECTANGLE)
    fWidthForCrHeight  =               B;

  return fWidthForCrHeight;

} //double TrayToolKit::widthForCrHeight(double Hcr, double B, double M)

double TrayToolKit::crHydraulicInclination(double Hcrp, double Bcr, double B, double X, double C, double M, double G, double A) {

  double fCrHydraulicInclination = 0;

  if (Hcrp == 0)
    Hcrp = crHeightRectTray();

  if (Bcr == 0)
    Bcr = widthForCrHeight();

  if (B == 0)
    B = getVar(VAR_B);

  if (X == 0)
    X = moistenedPerimeter();

  if (C == 0)
    C = shezy();

  if (M == 0)
    M = getVar(VAR_M);

  if (G == 0)
    G = getVar(VAR_G);

  if (A == 0)
    A = getVar(VAR_A);

  fCrHydraulicInclination = ( G * X * (3 * B  -  0.685 * M * Hcrp) ) / ( 3 * B * A * Bcr * pow(C, 2)  );

  return fCrHydraulicInclination;
  
} //double TrayToolKit::crHydraulicInclination(double Hcrp, double Bcr, double B, double X, double C, double G, double A)

double TrayToolKit::calcAQ(double P, double Mr, double Cy, double Cn, double Q20) {

  double A = 0;

  if (P == 0)
    P = getVar(VAR_P);

  if (Mr == 0)
    Mr = getVar(VAR_MR);

  if (Cy == 0)
    Cy = getVar(VAR_CY);

  if (Cn == 0)
    Cn = getVar(VAR_CN);

  if (Q20 == 0)
    Q20 = getVar(VAR_Q20);

  A = Q20 * pow(20, Cn) *  pow(   (1 + (log10(P) / log10(Mr)) )  ,Cy);

  return A;

} //double TrayToolKit::calcAQ(double P, double Mr, double Cy, double Cn, double Q20)

double TrayToolKit::rainWaterConsumption(double P, double F, double TimeRain, double Kpt, double Kpf, double Mr,
					 double Zmid, double Cy, double Cn, double Q20) {
  
  double fRainWaterConsumption = 0;
  double A = 0;

  if (P == 0)
    P = getVar(VAR_P);

  if (F == 0)
    F = getVar(VAR_F);

  if (TimeRain == 0)
    TimeRain = environment(VAR_TIMERAIN);

  if (Kpt == 0)
    Kpt = getVar(VAR_KPT);

  if (Kpf == 0)
    Kpf = getVar(VAR_KPF);

  if (Mr == 0)
    Mr = getVar(VAR_MR);

  if (Zmid == 0)
    Zmid = getVar(VAR_ZMID);

  if (Cy == 0)
    Cy = getVar(VAR_CY);

  if (Cn == 0)
    Cn = getVar(VAR_CN);

  if (Q20 == 0)
    Q20 = getVar(VAR_Q20);

  A = calcAQ(); 

  //Расхода дождевых вод
  fRainWaterConsumption = Kpt * Kpf * F *  (   (Zmid  * pow(A, 1.2)) / ( pow(TimeRain, 1.2*Cn - 0.1) )   );
 
  //Перевод в м3/с
  fRainWaterConsumption /= 1000;

  return fRainWaterConsumption;

} //double TrayToolKit::rainWaterConsumption(double P, double F, double TimeRain, double Kpt, double Kpf, double Mr...

// double TrayToolKit::extHeightTrapTray() {

void TrayToolKit::updateHrain() {

  double fExtHeightTrapTray = 0;
  
  double Hmax = getVar(VAR_H);
  double q    = rainWaterConsumption();
  double N    = getVar(VAR_N);
  double I    = getVar(VAR_I);

  double S    = 0;
  double X    = 0;
  double R    = 0;
  double C    = 0;
  double y    = 0;
  double expQ = 0;

  for (double curH = 0.0; curH <= Hmax; curH += 0.001) {

    S = areaLiveProfile(curH);
    X = moistenedPerimeter(curH);
    R = S / X;

    y = 2.5 * sqrt(N) - 0.13 - 0.75 * sqrt(R) * (sqrt(N) - 0.1);
    C = (  1 / N  )  *  pow(R, y);

    expQ = S * C * sqrt(R * I);

    if (  fabs(q - expQ) < 0.001) { 

      fExtHeightTrapTray = curH;
      break;

    } //if (  fabs(q - expQ) < 0.01)

  } //for (double curH = 0; curH <= insH; curH += 0.01)

  setVar(fExtHeightTrapTray, VAR_HRAIN);

} //void TrayToolKit::updateHrain()

void TrayToolKit::updateN() {

  switch (insTypeConcrete) {

    case 1: setVar(0.014, VAR_N); break;
    case 2: setVar(0.017, VAR_N); break;
    case 3: setVar(0.015, VAR_N); break;
    case 4: setVar(0.016, VAR_N); break;
    case 5: setVar(0.035, VAR_N); break;

  } //switch (insTypeConcrete)

} //void TrayToolKit::updateN()

void TrayToolKit::updateCnMrCy() {

  double P = getVar(VAR_P);

  switch (insRegion) {

    case 1:   if (P >= 1) setVar(0.40, VAR_CN); else setVar(0.35, VAR_CN); setVar(130, VAR_MR); setVar(1.33, VAR_CY); break;
    case 2:   if (P >= 1) setVar(0.62, VAR_CN); else setVar(0.48, VAR_CN); setVar(120, VAR_MR); setVar(1.33, VAR_CY); break;
    case 3:   if (P >= 1) setVar(0.71, VAR_CN); else setVar(0.59, VAR_CN); setVar(150, VAR_MR); setVar(1.54, VAR_CY); break;
    case 4:   if (P >= 1) setVar(0.71, VAR_CN); else setVar(0.64, VAR_CN); setVar(110, VAR_MR); setVar(1.54, VAR_CY); break;
    case 5:   if (P >= 1) setVar(0.71, VAR_CN); else setVar(0.59, VAR_CN); setVar(150, VAR_MR); setVar(1.54, VAR_CY); break;
    case 6:   if (P >= 1) setVar(0.67, VAR_CN); else setVar(0.57, VAR_CN); setVar(60, VAR_MR);  setVar(1.82, VAR_CY); break;
    case 7:   if (P >= 1) setVar(0.66, VAR_CN); else setVar(0.66, VAR_CN); setVar(50, VAR_MR);  setVar(2.00, VAR_CY); break;
    case 8:   if (P >= 1) setVar(0.70, VAR_CN); else setVar(0.66, VAR_CN); setVar(70, VAR_MR);  setVar(1.54, VAR_CY); break;
    case 9:   if (P >= 1) setVar(0.63, VAR_CN); else setVar(0.56, VAR_CN); setVar(100, VAR_MR); setVar(1.82, VAR_CY); break;
    case 10:  if (P >= 1) setVar(0.72, VAR_CN); else setVar(0.58, VAR_CN); setVar(80, VAR_MR);  setVar(1.54, VAR_CY); break;

    case 11:  if (P >= 1) setVar(0.74, VAR_CN); else setVar(0.66, VAR_CN); setVar(80, VAR_MR);  setVar(1.82, VAR_CY); break;
    case 12:  if (P >= 1) setVar(0.57, VAR_CN); else setVar(0.57, VAR_CN); setVar(80, VAR_MR);  setVar(1.33, VAR_CY); break;
    case 13:  if (P >= 1) setVar(0.61, VAR_CN); else setVar(0.48, VAR_CN); setVar(140, VAR_MR); setVar(1.33, VAR_CY); break;
    case 14:  if (P >= 1) setVar(0.49, VAR_CN); else setVar(0.33, VAR_CN); setVar(100, VAR_MR); setVar(1.54, VAR_CY); break;
    case 15:  if (P >= 1) setVar(0.69, VAR_CN); else setVar(0.47, VAR_CN); setVar(130, VAR_MR); setVar(1.54, VAR_CY); break;
    case 16:  if (P >= 1) setVar(0.48, VAR_CN); else setVar(0.35, VAR_CN); setVar(130, VAR_MR); setVar(1.82, VAR_CY); break;
    case 17:  if (P >= 1) setVar(0.60, VAR_CN); else setVar(0.52, VAR_CN); setVar(90, VAR_MR);  setVar(1.54, VAR_CY); break;
    case 18:  if (P >= 1) setVar(0.65, VAR_CN); else setVar(0.54, VAR_CN); setVar(100, VAR_MR); setVar(1.54, VAR_CY); break;
    case 19:  if (P >= 1) setVar(0.36, VAR_CN); else setVar(0.48, VAR_CN); setVar(100, VAR_MR); setVar(1.54, VAR_CY); break;
    case 20:  if (P >= 1) setVar(0.35, VAR_CN); else setVar(0.31, VAR_CN); setVar(80, VAR_MR);  setVar(1.54, VAR_CY); break;

    case 21:  if (P >= 1) setVar(0.28, VAR_CN); else setVar(0.26, VAR_CN); setVar(110, VAR_MR); setVar(1.54, VAR_CY); break;
    case 22:  if (P >= 1) setVar(0.35, VAR_CN); else setVar(0.28, VAR_CN); setVar(110, VAR_MR); setVar(1.54, VAR_CY); break;
    case 23:  if (P >= 1) setVar(0.65, VAR_CN); else setVar(0.57, VAR_CN); setVar(90, VAR_MR);  setVar(1.54, VAR_CY); break;
    case 24:  if (P >= 1) setVar(0.45, VAR_CN); else setVar(0.44, VAR_CN); setVar(110, VAR_MR); setVar(1.54, VAR_CY); break;
    case 25:  if (P >= 1) setVar(0.44, VAR_CN); else setVar(0.40, VAR_CN); setVar(40, VAR_MR);  setVar(1.82, VAR_CY); break;
    case 26:  if (P >= 1) setVar(0.41, VAR_CN); else setVar(0.37, VAR_CN); setVar(40, VAR_MR);  setVar(1.54, VAR_CY); break;
    case 27:  if (P >= 1) setVar(0.49, VAR_CN); else setVar(0.32, VAR_CN); setVar(20, VAR_MR);  setVar(1.54, VAR_CY); break;
    case 28:  if (P >= 1) setVar(0.62, VAR_CN); else setVar(0.58, VAR_CN); setVar(90, VAR_MR);  setVar(1.54, VAR_CY); break;
    case 29:  if (P >= 1) setVar(0.51, VAR_CN); else setVar(0.43, VAR_CN); setVar(60, VAR_MR);  setVar(1.82, VAR_CY); break;
    case 30:  if (P >= 1) setVar(0.58, VAR_CN); else setVar(0.47, VAR_CN); setVar(70, VAR_MR);  setVar(1.82, VAR_CY); break;

    case 31:  if (P >= 1) setVar(0.57, VAR_CN); else setVar(0.52, VAR_CN); setVar(100, VAR_MR); setVar(1.54, VAR_CY); break;
    case 32:  if (P >= 1) setVar(0.54, VAR_CN); else setVar(0.50, VAR_CN); setVar(90, VAR_MR);  setVar(1.33, VAR_CY); break;
    case 33:  if (P >= 1) setVar(0.63, VAR_CN); else setVar(0.52, VAR_CN); setVar(90, VAR_MR);  setVar(1.33, VAR_CY); break;
    case 34:  if (P >= 1) setVar(0.67, VAR_CN); else setVar(0.53, VAR_CN); setVar(100, VAR_MR); setVar(1.33, VAR_CY); break;
    case 35:  if (P >= 1) setVar(0.44, VAR_CN); else setVar(0.38, VAR_CN); setVar(171, VAR_MR); setVar(2.20, VAR_CY); break;

  } //switch (insRegion)

} //void TrayToolKit::updateCnMrCy()

void TrayToolKit::updateZmid() {

  switch (insTypeUpside) {

    case 1: setVar(0.224, VAR_ZMID); break;
    case 2: setVar(0.145, VAR_ZMID); break;
    case 3: setVar(0.125, VAR_ZMID); break;
    case 4: setVar(0.090, VAR_ZMID); break;
    case 5: setVar(0.064, VAR_ZMID); break;
    case 6: setVar(0.038, VAR_ZMID); break;

  } //switch (insTypeUpside)

} //void TrayToolKit::updateZmid()

void TrayToolKit::updateKpf() {

  if		     ( insF <  800 )			setVar(1.00, VAR_KPF);
  else if ((insF >= 800 )  && ((insF < 1000 )))		setVar(0.95, VAR_KPF);
  else if ((insF >= 1000)  && ((insF < 2000 )))		setVar(0.90, VAR_KPF);
  else if ((insF >= 2000)  && ((insF < 4000 ))) 	setVar(0.85, VAR_KPF);
  else if ((insF >= 4000)  && ((insF < 6000 )))		setVar(0.80, VAR_KPF);
  else if ((insF >= 6000)  && ((insF < 8000 )))		setVar(0.70, VAR_KPF);
  else if ((insF >= 8000)  && ((insF < 10000)))		setVar(0.60, VAR_KPF);
  else if            (insF >= 10000) 	 		setVar(0.55, VAR_KPF);

} //void TrayToolKit::updatePk()

void TrayToolKit::updateKpt() {

  if                  ( insTimeRain < 6 )		setVar(0.80, VAR_KPT);
  else if ((insTimeRain >= 6) && (insTimeRain < 8)) 	setVar(0.90, VAR_KPT);
  else if             (insTimeRain >= 8 )  	      	setVar(1.00, VAR_KPT);

} //void TrayToolKit::updateKr()

void TrayToolKit::updateVd() {

  double Hrain = getVar(VAR_HRAIN);
  int    ClassConcrete = environment(VAR_CLCONCRETE);
  double Vd = 0.0;

//Первый класс бетона
  if      (ClassConcrete == 1) {

    if                (Hrain <= 0.6)
      Vd = 0.6;
    else if ((Hrain > 0.6 ) && (Hrain <= 1.0))
      Vd = 10.6;
    else if ((Hrain > 1.0 ) && (Hrain <= 3.0))
      Vd = 12.3;
    else if ((Hrain > 3.0 ) && (Hrain <= 6.0))
      Vd = 13.0;

  } //if (ClassConcrete == 1)

//Второй класс бетона
  else if (ClassConcrete == 2) {

    if                (Hrain <= 0.6)
      Vd = 11.2;
    else if ((Hrain > 0.6 ) && (Hrain <= 1.0))
      Vd = 12.4;
    else if ((Hrain > 1.0 ) && (Hrain <= 3.0))
      Vd = 14.3;
    else if ((Hrain > 3.0 ) && (Hrain <= 6.0))
      Vd = 15.2;

  } //else if (ClassConcrete == 2)

//Третий класс бетона
  else if (ClassConcrete == 3) {

    if                (Hrain <= 0.6)
      Vd = 12.5;
    else if ((Hrain > 0.6 ) && (Hrain <= 1.0))
      Vd = 13.8;
    else if ((Hrain > 1.0 ) && (Hrain <= 3.0))
      Vd = 16.0;
    else if ((Hrain > 3.0 ) && (Hrain <= 6.0))
      Vd = 17.0;

  } //else if (ClassConcrete == 3)

//Четвертый класс бетона
  else if (ClassConcrete == 4) {

    if                (Hrain <= 0.6)
      Vd = 14.0;
    else if ((Hrain > 0.6 ) && (Hrain <= 1.0))
      Vd = 15.6;
    else if ((Hrain > 1.0 ) && (Hrain <= 3.0))
      Vd = 18.0;
    else if ((Hrain > 3.0 ) && (Hrain <= 6.0))
      Vd = 19.1;

  } //else if (ClassConcrete == 4)

//Пятый класс бетона
  else if (ClassConcrete == 5) {

    if                (Hrain <= 0.6)
      Vd = 15.6;
    else if ((Hrain > 0.6 ) && (Hrain <= 1.0))
      Vd = 17.3;
    else if ((Hrain > 1.0 ) && (Hrain <= 3.0))
      Vd = 20.0;
    else if ((Hrain > 3.0 ) && (Hrain <= 6.0))
      Vd = 21.2;

  } //else if (ClassConcrete == 5)

//Шестой класс бетона
  else if (ClassConcrete == 6) {

    if                (Hrain <= 0.6)
      Vd = 19.2;
    else if ((Hrain > 0.6 ) && (Hrain <= 1.0))
      Vd = 21.2;
    else if ((Hrain > 1.0 ) && (Hrain <= 3.0))
      Vd = 24.6;
    else if ((Hrain > 3.0 ) && (Hrain <= 6.0))
      Vd = 26.1;

  } //else if (ClassConcrete == 6)

  setVar(Vd, VAR_VD);

} //void TrayToolKit::updateVd()

QString TrayToolKit::getNameClassConcrete() {

  int ClassConcrete = environment(VAR_CLCONCRETE);

  switch (ClassConcrete) {

     case 1: return "B2.5"; break;
     case 2: return "B5"; break;
     case 3: return "B7.5"; break;
     case 4: return "B10"; break;
     case 5: return "B12.5"; break;
     case 6: return "B15"; break;

  } //switch (ClassConcrete)

  return "";

} //QString TrayToolKit::getNameClassConcrete()

void TrayToolKit::updateVs() {

  double Vs = 0;
  double R = 0;

  R = hydraulicRadius(getVar(VAR_HRAIN));

  Vs = 0.3 * pow(R, 0.25);

  setVar(Vs, VAR_VS);
  
} //void TrayToolKit::updateVs()

void TrayToolKit::updateVr() {

  double Vr = 0;
  double C  = 0;
  double R  = 0;
  double I  = 0;

  C = shezy(getVar(VAR_HR));
  R = hydraulicRadius(getVar(VAR_HR));
  I = getVar(VAR_IR);

  Vr = C * sqrt(R * I);

  setVar(Vr, VAR_VR);

} //void TrayToolKit::updateVr()
